package top.revery.admin.controller_service;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.revery.admin.util.FeignServiceMapping;
import top.revery.api.config.HttpResponResult;
import top.revery.api.config.ResultType;
import top.revery.api.entity.user.BaseUserInfo;

import java.util.Date;

/**
 * <h1> ---  --- </h1>
 * Created by  revery.top jun
 * Date  2019-12-17 17:09
 */
@RestController
@RequestMapping("/user")
public class UserWebController {

    /*
    * 获取用户信息
    * */
    @GetMapping("/getUserInfo")
    Object getUserInfo(){
//        BaseUserInfo userInfo = FeignServiceMapping.userServiceDataSource.findById(22l);



        return null;
    }

    /*
    * 用户登录
    * */
    @PostMapping("/login")
    HttpResponResult login(String name,String password){
        HttpResponResult httpResponResult = null;
        try{
            BaseUserInfo userInfo = FeignServiceMapping.uDataSource.login(name,password);
            httpResponResult = new HttpResponResult(ResultType.SUCCESS);
            httpResponResult.put("user_info",userInfo);
        }catch (Exception e){
            httpResponResult = new HttpResponResult(ResultType.ERROR_SERVICE,e.getMessage());
            httpResponResult.exception = e;
        }
        return httpResponResult;
    }

    /*
    * 用户注册
    * */
    @PostMapping("/register")
    HttpResponResult register(int userType,String name,String tel,String code,String password,@RequestParam(defaultValue = "") String email){
        HttpResponResult httpResponResult = null;
        try{
            //达人注册时，自动补充会员名称
            if(StringUtils.isEmpty(name)){
                name = "ID_"+new Date().getTime();
            }

//            BaseUserInfo baseUserInfo = FeignServiceMapping.userServiceDataSource.register(code,name,tel,userType,password,email);
//            System.out.println(baseUserInfo);
            httpResponResult = new HttpResponResult(ResultType.SUCCESS);
        }catch (Exception e){
            httpResponResult = new HttpResponResult(ResultType.ERROR_SERVICE,e.getMessage());
            httpResponResult.exception = e;
        }
        return httpResponResult;
    }


    /*
     * 修改密码
     * */
    @PostMapping("/updatePassword")
    HttpResponResult updatePassword(String tel,String code,String password){
        HttpResponResult httpResponResult = null;
        try{

//            if(!FeignServiceMapping.userServiceDataSource.forget(code,tel,password)){
//                throw new Exception("服务器修改失败！");
//            }
            httpResponResult = new HttpResponResult(ResultType.SUCCESS);
        }catch (Exception e){
            httpResponResult = new HttpResponResult(ResultType.ERROR_SERVICE,e.getMessage());
            httpResponResult.exception = e;
        }
        return httpResponResult;
    }
}
